package com.music.note_android.ui.folder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.music.note_android.R
import com.music.note_android.data.model.Folder

class FolderAdapter(
    private val onFolderClick: (Folder) -> Unit,
    private val onRenameFolder: (Folder) -> Unit,
    private val onDeleteFolder: (Folder) -> Unit,
    private val onMoveNotesToFolder: (Folder) -> Unit,
    private val onViewFolderNotes: (Folder) -> Unit
) : ListAdapter<Folder, FolderAdapter.FolderViewHolder>(FolderDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FolderViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_folder, parent, false)
        return FolderViewHolder(view)
    }

    override fun onBindViewHolder(holder: FolderViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    inner class FolderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val folderIcon: ImageView = itemView.findViewById(R.id.iv_folder_icon)
        private val folderName: TextView = itemView.findViewById(R.id.tv_folder_name)
        private val noteCount: TextView = itemView.findViewById(R.id.tv_note_count)
        private val createdTime: TextView = itemView.findViewById(R.id.tv_created_time)
        private val systemFolderBadge: TextView = itemView.findViewById(R.id.tv_system_folder_badge)
        private val moreOptionsButton: ImageButton = itemView.findViewById(R.id.btn_more_options)

        fun bind(folder: Folder) {
            // 设置文件夹名称
            folderName.text = folder.name
            
            // 设置笔记数量
            noteCount.text = folder.getNoteCountText()
            
            // 设置创建时间
            createdTime.text = folder.getFormattedCreatedTime()
            
            // 设置系统文件夹标识
            if (folder.isSystem) {
                systemFolderBadge.visibility = View.VISIBLE
                folderIcon.setImageResource(R.drawable.ic_folder_24)
            } else {
                systemFolderBadge.visibility = View.GONE
                folderIcon.setImageResource(R.drawable.ic_folder_24)
            }
            
            // 设置点击事件
            itemView.setOnClickListener {
                onFolderClick(folder)
            }
            
            // 设置更多选项菜单
            moreOptionsButton.setOnClickListener { view ->
                showContextMenu(view, folder)
            }
        }
        
        private fun showContextMenu(view: View, folder: Folder) {
            val popup = PopupMenu(view.context, view)
            popup.menuInflater.inflate(R.menu.folder_context_menu, popup.menu)
            
            // 根据文件夹类型调整菜单项可见性
            val renameItem = popup.menu.findItem(R.id.action_rename_folder)
            val deleteItem = popup.menu.findItem(R.id.action_delete_folder)
            
            renameItem.isVisible = folder.canRename()
            deleteItem.isVisible = folder.canDelete()
            
            popup.setOnMenuItemClickListener { menuItem ->
                when (menuItem.itemId) {
                    R.id.action_rename_folder -> {
                        onRenameFolder(folder)
                        true
                    }
                    R.id.action_delete_folder -> {
                        onDeleteFolder(folder)
                        true
                    }
                    R.id.action_move_notes_to_folder -> {
                        onMoveNotesToFolder(folder)
                        true
                    }
                    R.id.action_view_folder_notes -> {
                        onViewFolderNotes(folder)
                        true
                    }
                    else -> false
                }
            }
            
            popup.show()
        }
    }

    class FolderDiffCallback : DiffUtil.ItemCallback<Folder>() {
        override fun areItemsTheSame(oldItem: Folder, newItem: Folder): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Folder, newItem: Folder): Boolean {
            return oldItem == newItem
        }
    }
}